//
//  LXHistoryTableViewCell.m
//  cat
//
//  Created by imera on 16/3/30.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXHistoryTableViewCell.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"

#define LXHistoryTableViewCellMargin 10

@interface LXHistoryTableViewCell()

@property (nonatomic, weak) UIImageView *videoImageView;

@property (nonatomic, weak) UILabel *titleLabel;

@property (nonatomic, weak) UILabel *timeLabel;

@end

@implementation LXHistoryTableViewCell

- (void)setDict:(NSDictionary *)dict
{
    if (dict != nil) {
        _dict = dict;
        [self layoutSubviews];
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubViews];
        self.backgroundColor = MainBackgroundColor;
    }
    return self;
}

- (void)initSubViews
{
    UIImageView *videoImageView = [[UIImageView alloc]init];
    self.videoImageView = videoImageView;
    [self addSubview:videoImageView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = [UIFont boldSystemFontOfSize:13];
    self.titleLabel = titleLabel;
    [self addSubview:titleLabel];
    
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.font = [UIFont boldSystemFontOfSize:13];
    self.timeLabel = timeLabel;
    [self addSubview:timeLabel];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    UIImageView *videoImageView = self.videoImageView;
    [videoImageView sd_setImageWithURL:[NSURL URLWithString:self.dict[@"videoThumb"]]];
    CGFloat videoImageViewX = LXHistoryTableViewCellMargin;
    CGFloat videoImageViewH = self.frame.size.height - 10;
    CGFloat videoImageViewY = (self.frame.size.height - videoImageViewH) / 2;
    CGFloat videoImageViewW = 100;
    videoImageView.frame = CGRectMake(videoImageViewX, videoImageViewY, videoImageViewW, videoImageViewH);
    
    UILabel *titleLabel = self.titleLabel;
    titleLabel.text = self.dict[@"videoName"];
    
    CGSize titleLabelSize = [titleLabel.text sizeWithAttributes:@{NSFontAttributeName:titleLabel.font}];
    CGFloat titleLabelX = CGRectGetMaxX(videoImageView.frame) + LXHistoryTableViewCellMargin;
    CGFloat titleLabelY = videoImageViewY;
    CGFloat titleLabelW = self.frame.size.width - videoImageViewW - LXHistoryTableViewCellMargin * 3;
    titleLabel.numberOfLines = (titleLabelSize.width / titleLabelW) + 1;
    CGFloat titleLabelH = titleLabelSize.height * titleLabel.numberOfLines;
    titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    UILabel *timeLabel = self.timeLabel;
    timeLabel.text = self.dict[@"videoTime"];
    
    CGSize timeLabelSize = [timeLabel.text sizeWithAttributes:@{NSFontAttributeName:timeLabel.font}];
    CGFloat timeLabelX = titleLabelX;
    CGFloat timeLabelW = self.frame.size.width - timeLabelX - LXHistoryTableViewCellMargin;
    CGFloat timeLabelH = timeLabelSize.height;
    CGFloat timeLabelY = CGRectGetMaxY(videoImageView.frame) - timeLabelH;
    timeLabel.frame = CGRectMake(timeLabelX, timeLabelY, timeLabelW, timeLabelH);
}
@end
